#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../src/string.h"
#include "../src/dict.h"

/*
 * dict_t testing unit
 * compile with: gcc ../src/string.o ../src/array.o ../src/xmalloc.o ../src/dict.o -lm dictest.c -o dictest
 */

#define TEST_KEY0 "First key."
#define TEST_STR0 "This is a simple test string."
#define TEST_KEY1 "Second key."
#define TEST_STR1 "This should be the value of the second key."

int
main(int argc, char **argv)
{
  const char *key0 = TEST_KEY0;
  const char *val0 = TEST_STR0;
  const char *key1 = TEST_KEY1;
  const char *val1 = TEST_STR1;
  const char *tmp = NULL;
  dict_t *dict = NULL;

  dict = create_dict(true);
  insert(dict, TEST_KEY0, strlen(TEST_KEY0), TEST_STR0, strlen(TEST_STR0));
  insert(dict, TEST_KEY1, strlen(TEST_KEY1), TEST_STR1, strlen(TEST_STR1));
  if (dict_len(dict) != 2) return (1);
  if ( !(tmp = lookup(dict, TEST_KEY1)) ) return (2);
  if ( strlen(TEST_STR1) != strlen(tmp) ) return (3);
  free_dict(dict);

  dict = create_dict(false);
  insert(dict, key0, strlen(key0), val0, strlen(val0));
  insert(dict, key1, strlen(key1), val1, strlen(val1));
  if (dict_len(dict) != 2) return (4);
  if ( !(tmp = lookup(dict, key1)) ) return (5);
  if ( strlen(val1) != strlen(tmp) ) return (6);
  free_dict(dict);

  return (0);
}

